
package com.feib.soeasy.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.feib.soeasy.job.AutoUploadCarLoanFileJob;
import com.feib.soeasy.job.ForceLogoutJob;
import com.feib.soeasy.service.ScheduleService;
import com.feib.soeasy.util.SpringContextUtil;


/**
 * @title (#)ScheduleServiceImpl.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/12/16 create by Jimmy Liu<br>
 */
@Service("scheduleService")
public class ScheduleServiceImpl extends BaseServiceImpl implements ScheduleService {

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.ScheduleService#doForceLogout()
     */
    @Override
    @Scheduled(fixedDelay=180000) // 3 分鐘起來一次
    public void doForceLogout() {
        Date start = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        try {
            ForceLogoutJob.logger.info("強迫登出開始 -" + dateFormat.format(start));
            ForceLogoutJob forceLogoutJob = (ForceLogoutJob)SpringContextUtil.getBean("forceLogoutJob");
            forceLogoutJob.execute(null);
        }
        catch (Exception e) {
            ForceLogoutJob.logger.error("強迫登除排程執行失敗", e);
        }
        finally{
            Date end = new Date();
            ForceLogoutJob.logger.info("強迫登出結束 -" + dateFormat.format(end));
            ForceLogoutJob.logger.info("共費時  - " + ((end.getTime() - start.getTime()) / 1000) + " 秒 ");
        }

    }

	@Override
    @Scheduled(cron="0 0/30 * * * * ") // 每30分鐘起來一次(00:30, 01:00, 01:30 ...)
	public void doAutoUploadCarLoanFile() {
        Date start = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        AutoUploadCarLoanFileJob autoUploadCarLoanFileJob = null;
        try {
        	AutoUploadCarLoanFileJob.logger.info("中租帳單自動上傳開始 -" + dateFormat.format(start));
        	autoUploadCarLoanFileJob = (AutoUploadCarLoanFileJob)SpringContextUtil.getBean("autoUploadCarLoanFileJob");
        	autoUploadCarLoanFileJob.execute();
        }
        catch (Exception e) {
        	AutoUploadCarLoanFileJob.logger.error("中租帳單自動上傳排程執行失敗", e);
        	if (null != autoUploadCarLoanFileJob)
        	{
        		try {
					autoUploadCarLoanFileJob.exceptionNotice(e, start);
				} catch (IOException e1) {
		        	AutoUploadCarLoanFileJob.logger.error("中租帳單自動上傳排程發送系統錯誤Mail失敗", e1);
				}
        	}
        		
        }
        finally{
            Date end = new Date();
            AutoUploadCarLoanFileJob.logger.info("中租帳單自動上傳結束 -" + dateFormat.format(end));
            AutoUploadCarLoanFileJob.logger.info("共費時  - " + ((end.getTime() - start.getTime()) / 1000) + " 秒 ");
        }
		
		
	}

}
